<?php

namespace App\Api\Controllers\YSEPay;


use Illuminate\Support\Facades\DB;

class StoreActionController
{

    private function _exception($msg = '', $code = 0)
    {
        throw new \Exception($msg, $code);
    }

    public function getConfig($store_id = '', $pid = '')
    {
        $merc_id = DB::table('ysepay_stores')->where('store_id', $store_id)->value('merc_id');
        if (!$merc_id) {
            $store_id = DB::table('stores')->where('id', $pid)->value('store_id');
            if (!$store_id) {
                $this->_exception("商户配置不存在");
            }
            $merc_id = DB::table('ysepay_stores')->where('store_id', $store_id)->value('merc_id');
        }
        if (!$merc_id) {
            $this->_exception('商户配置不存在');
        }
        return $merc_id;
    }

    public function qrCodePay($ways_type, $merc_id = '', $data = [])
    {
        $current_time = time();
        $out_trade_no = date('YmdHis', $current_time) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
        $data_insert = $data;
        $data_insert['ways_type'] = $ways_type;
        $bank_type = '';
        //支付宝
        if ($ways_type == '39105') {
            //入库参数
            $data_insert['ways_type_desc'] = '支付宝';
            $data_insert['ways_source'] = 'alipay';
            $data_insert['ways_source_desc'] = '支付宝';
            $data_insert['out_trade_no'] = $out_trade_no;
            $bank_type = '1903000';
        }
        //微信支付
        if ($ways_type == '39104') {
            //入库参数
            $data_insert['ways_type_desc'] = '微信支付';
            $data_insert['ways_source'] = 'weixin';
            $data_insert['ways_source_desc'] = '微信支付';
            $data_insert['out_trade_no'] = $out_trade_no;
            $bank_type = '1904000';
        }
        //云闪付
        if ($ways_type == '39106') {
            //入库参数
            $data_insert['ways_type_desc'] = '云闪付';
            $data_insert['ways_source'] = 'unionpay';
            $data_insert['ways_source_desc'] = '云闪付';
            $data_insert['out_trade_no'] = $out_trade_no;
            $bank_type = '9001002';
        }
        $order_id = DB::table('orders')->insertGetId($data_insert);
        if (!$order_id) {
            throw new \Exception("订单创建失败");
        }
        $obj = new PayController();
        $url = $obj->QrCodePay(['ways_type' => $ways_type, 'order_id' => $order_id], [
            'out_trade_no' => $out_trade_no,
            'shopdate' => date('Ymd', $current_time),
            'subject' => $data_insert['store_name'],
            'total_amount' => $data_insert['total_amount'],
            'seller_id' => $merc_id,
            'seller_name' => $data_insert['store_name'],
            'timeout_express' => '2h',
            'business_code' => '00510030',//业务代码 ，请联系银盛客户经理获取。
            //业务代码
            //微信公众号/小程序支付、支付宝生活号支付、二维码支付、银联行业码支付、反扫码支付（T1/D1）：00510030
            //微信公众号/小程序支付、支付宝生活号支付、二维码支付、银联行业码支付（D0）：00510032
            //反扫码支付（D0）：00510080」
            'bank_type' => $bank_type,//二维码行别
            "currency" => "CNY",
        ]);
        return $url;
    }

    public function barCodePay($ways_type, $merc_id = '', $auth_code = '', $data = [])
    {
        $current_time = time();
        $out_trade_no = date('YmdHis', $current_time) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
        $data_insert = $data;
        $data_insert['ways_type'] = $ways_type;
        $bank_type = '';
        //支付宝
        if ($ways_type == '39105') {
            //入库参数
            $data_insert['ways_type_desc'] = '支付宝';
            $data_insert['ways_source'] = 'alipay';
            $data_insert['ways_source_desc'] = '支付宝';
            $data_insert['out_trade_no'] = $out_trade_no;
            $bank_type = '1903000';
        }
        //微信支付
        if ($ways_type == '39104') {
            //入库参数
            $data_insert['ways_type_desc'] = '微信支付';
            $data_insert['ways_source'] = 'weixin';
            $data_insert['ways_source_desc'] = '微信支付';
            $data_insert['out_trade_no'] = $out_trade_no;
            $bank_type = '1904000';
        }
        //云闪付
        if ($ways_type == '39106') {
            //入库参数
            $data_insert['ways_type_desc'] = '云闪付';
            $data_insert['ways_source'] = 'unionpay';
            $data_insert['ways_source_desc'] = '云闪付';
            $data_insert['out_trade_no'] = $out_trade_no;
            $bank_type = '9001002';
        }
        $order_id = DB::table('orders')->insertGetId($data_insert);
        if (!$order_id) {
            throw new \Exception("订单创建失败");
        }
        $obj = new PayController();
        $url = $obj->BarCodePay(['ways_type' => $ways_type, 'order_id' => $order_id], [
            'out_trade_no' => $out_trade_no,
            'shopdate' => date('Ymd', $current_time),
            'subject' => $data_insert['store_name'],
            'total_amount' => $data_insert['total_amount'],
            'seller_id' => $merc_id,
            'seller_name' => $data_insert['store_name'],
            'timeout_express' => '2h',
            'business_code' => '00510030',//业务代码 ，请联系银盛客户经理获取。
            'auth_code' => $auth_code,//扫码支付授权码
            //业务代码
            //微信公众号/小程序支付、支付宝生活号支付、二维码支付、银联行业码支付、反扫码支付（T1/D1）：00510030
            //微信公众号/小程序支付、支付宝生活号支付、二维码支付、银联行业码支付（D0）：00510032
            //反扫码支付（D0）：00510080」
            'bank_type' => $bank_type,//二维码行别
            "currency" => "CNY",
        ]);
    }

    public function qrderQuery($orderNo)
    {
        $obj = new PayController();
//        $result = $obj->QrCodePay([
//            'out_trade_no' => $orderNo,
//        ]);
    }


}
